package com.zxj.schoolxianyu.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zxj.schoolxianyu.dao.browsinghistoryDao;
import com.zxj.schoolxianyu.entity.browsinghistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class addBrowseHisService {
    @Autowired(required = false)
    private browsinghistoryDao browsinghistoryDao;

    public String addHis(String username, Integer goodsid) {
        EntityWrapper<browsinghistory> b1 = new EntityWrapper<>();
        b1.eq("username", username);
        b1.eq("goodsid", goodsid);
        List<browsinghistory> bs = browsinghistoryDao.selectList(b1);
        if (bs.isEmpty()) {
            return insert(username, goodsid);
        } else {
            browsinghistoryDao.delete(b1);
            return insert(username, goodsid);
        }
    }

    private String insert(String username, Integer goodsid) {
        browsinghistory b = new browsinghistory();
        b.username = username;
        b.goodsid = goodsid;
        browsinghistoryDao.insert(b);
        return "success";
    }
}
